package com.cai.xiaobai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cai.xiaobai.entity.AnswerInfo;
import com.cai.xiaobai.entity.AnswerOptionContent;
import com.cai.xiaobai.entity.DTO.answerInfo.fillInFrom.FillInFromVO;
import com.cai.xiaobai.entity.FromInfo;
import com.cai.xiaobai.mapper.AnswerInfoMapper;
import com.cai.xiaobai.result.R;
import com.cai.xiaobai.result.Result;
import com.cai.xiaobai.result.ResultCode;
import com.cai.xiaobai.service.IAnswerInfoService;
import com.cai.xiaobai.service.IAnswerOptionContentService;
import com.cai.xiaobai.utils.SnowFlow;
import com.cai.xiaobai.utils.common.AuthUtil;
import com.cai.xiaobai.utils.common.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 答案数据表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-13
 */
@Service
@AllArgsConstructor
public class AnswerInfoServiceImpl extends ServiceImpl<AnswerInfoMapper, AnswerInfo> implements IAnswerInfoService {

    private final AnswerInfoMapper answerInfoMapper;
    private final IAnswerOptionContentService iAnswerOptionContentService;

    @Override
    public Result getHistory(String openId) {
        List<FromInfo> fromInfoList = answerInfoMapper.getHistory(openId);
        return R.Success(fromInfoList);
    }

    @Override
    public Map<Long, Long> getFromAnswerNumber(List<Long> formIdList) {
        if (formIdList.isEmpty()) {
            return null;
        }
        List<AnswerInfo> answerInfoList = this.list(new QueryWrapper<AnswerInfo>()
            .lambda().in(AnswerInfo::getFromId, formIdList));
        return answerInfoList.stream()
            .collect(Collectors.groupingBy(AnswerInfo::getFromId, Collectors.counting()));
    }

    @Override
    public Map<Long, AnswerOptionContent> getFromAnswerInfo(String fromId) {
        List<AnswerOptionContent> answerInfo = answerInfoMapper.getFromAnswerInfo(fromId, AuthUtil.getOpenId());
        if (Func.isEmpty(answerInfo)) {
            return null;
        }
        HashMap<Long, AnswerOptionContent> map = new HashMap<>();
        answerInfo.forEach(answerOptionContent -> {
            map.put(answerOptionContent.getQuestionId(), answerOptionContent);
        });
        return map;
    }

    @Override
    public Result fillInFrom(FillInFromVO fillInFromVO) {
        SnowFlow snowFlow = new SnowFlow(1, 1);
        long nextId = snowFlow.nextId();
        AnswerInfo answerInfo = new AnswerInfo();
        answerInfo.setId(nextId);
        answerInfo.setFromId(fillInFromVO.getFromId());
        answerInfo.setCreateTime(LocalDateTime.now());
        answerInfo.setOpenId(AuthUtil.getOpenId());
        fillInFromVO.getAnsList().forEach(answerOptionContent -> {
            answerOptionContent.setAnswerId(nextId);
        });
        if (this.save(answerInfo) && iAnswerOptionContentService.saveBatch(fillInFromVO.getAnsList())) {
            return R.Success("修改成功");
        } else {
            return R.Fail(ResultCode.INTERNAL_SERVER_ERROR, "修改失败请联系管理人员");
        }
    }
}
